#include	<string.h>
#include	<stdio.h>
#include	<math.h>
#include	"Uigp/igp.h"

#include	"Umath/Matrix2Type.h"

#include	"Ucamera/Pt2dType.h"



int
pt2d_approximate_Ax_B( pt2dA_type *apt, int i0, int i1, float *a, float *b )
{
matrix2_type	m;
vec2d_type	D,	X;
float	x,	y;
int	i;


	pt2dA_dump( apt, "axb", 1, NULL );

	matrix2_zero( &m );
	D.x = D.y = 0;


	if( i0 < 0 )
		i0 = 0;

	if( i1 > apt->nP )
		i1 = apt->nP;


	for( i = i0 ; i < i1 ; i++ ){


		x = apt->p[i].p.x;
		y = apt->p[i].p.y;

	
		m.a00 += x*x;
		m.a01 += x;

		m.a11 += 1;


		D.x += y*x;
		D.y += y;
	}

	m.a10 = m.a01;




	matrix2S_solve( &m, &D, &X  );

	*a = X.x;
	*b = X.y;

	return( m.a11 );
}